package com.echo.fragment;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.echo.SportsApplication;
import com.echo.activity.R;
import com.echo.view.RoundedImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class AccountFragment extends Fragment {

    @InjectView(R.id.account_head_img)
    RoundedImageView accountHeadImg;
    @InjectView(R.id.account_nickname)
    TextView accountNickname;
    @InjectView(R.id.account_phone)
    TextView accountPhone;
    private View mView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_account, container, false);
        initView();
        ButterKnife.inject(this, mView);
        return mView;
    }

    private void initView() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (SportsApplication.getInstance().isLogin()) {
            byte[] image = SportsApplication.getInstance().getUserHeadImage();
            if (image != null && image.length > 0)
                accountHeadImg.setImageBitmap(BitmapFactory.decodeByteArray(image, 0, image.length));
            else {
                String url = SportsApplication.getInstance().getUserHeadImageUrl();
                if (!(url==null)&&!url.equals("")) {
                    ImageLoader imageLoader = ImageLoader.getInstance();
                    imageLoader.init(ImageLoaderConfiguration.createDefault(getActivity()));
                    imageLoader.displayImage(url, accountHeadImg);
                }
            }
            accountNickname.setText(SportsApplication.getInstance().getUserNickname());
            String phone = SportsApplication.getInstance().getUserPhone();
            if (phone == null || phone.equals(""))
                accountPhone.setVisibility(View.GONE);
            else {
                accountPhone.setText(phone);
                accountPhone.setVisibility(View.VISIBLE);
            }
        } else {
            accountPhone.setVisibility(View.GONE);
            accountNickname.setText("");
            accountHeadImg.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.fv));
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

}
